/**
 * 
 */
package datamodel.tree.linked;

import datamodel.tree.Tree;
import datamodel.tree.TreeNode;

/**
 * An implementation of the tree interface.<br>
 * The nodes are Linked to each other.
 * 
 * @author Kevin Munk, Jakob Karolus
 * @version 1.0
 * 
 */
public class LinkedTree extends Tree {
	
	private LinkedTreeNode currentNode;
	private LinkedTreeNode root;
	
	
	@Override
	public boolean moveToLeftNode() {

		if(hasLeftNode()) {
			currentNode = (LinkedTreeNode) currentNode.getLeftnode();
			return true;
		} else return false;
	}

	@Override
	public boolean moveToRightNode() {
		if(hasRightNode()) {
			currentNode = (LinkedTreeNode) currentNode.getRightnode();
			return true;
		} else return false;
	}

	@Override
	public boolean moveToParentNode() {
		if(hasParentNode()) {
			currentNode = (LinkedTreeNode) currentNode.getParent();
			return true;
		} else return false;
	}

	@Override
	public boolean hasLeftNode() {
		if(currentNode != null && currentNode.getLeftnode() != null){
			return true;
		} else return false;
	}

	@Override
	public boolean hasRightNode() {
		if(currentNode != null && currentNode.getRightnode()!=null){
			return true;
		}else return false;
	}

	@Override
	public boolean hasParentNode() {
		if(currentNode.getParent()!=null){
			return true;
		}else return false;
	}

	@Override
	public boolean setLeftNode(TreeNode node) {
		if(currentNode == null) {
			return false;
		}
		
		LinkedTreeNode linkedNode = (LinkedTreeNode) node;
		linkedNode.setParent(currentNode);
		currentNode.setLeftnode(linkedNode);
		return true;
	}

	@Override
	public boolean setRightNode(TreeNode node) {
		if(currentNode == null) {
			return false;
		}
		
		LinkedTreeNode linkedNode = (LinkedTreeNode) node;
		linkedNode.setParent(currentNode);
		currentNode.setRightnode(linkedNode);
		return true;
	}

	@Override
	public void setCurrentNode(TreeNode node) {
		LinkedTreeNode linkedNode = (LinkedTreeNode) node;
		
		if(root == null) {
			currentNode = linkedNode;
			root = linkedNode;
		} else {
			linkedNode.setParent(currentNode.getParent());
			currentNode = linkedNode;
		}
	}

	@Override
	public TreeNode getCurrentNode() {
		return currentNode;
	}

	@Override
	public boolean moveToRoot() {
		if(root != null){
			currentNode = root;
			return true;
		}else {
			return false;
		}
	}

	@Override
	public void clearTree() {
		currentNode = null;
		root = null;
	}
}
